local skel = fk.CreateSkill {
  name = "emo__silan",
}

skel:addEffect(fk.CardUsing, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and data.card:isCommonTrick() and not player:isNude() then
      local subcards = player.room:getSubcardsByRule(data.card, {Card.Processing})
      if #subcards == 0 then return false end
      local firstEvent = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        local use = e.data
        return use.card:isCommonTrick()
      end, Player.HistoryTurn)[1]
      return firstEvent and firstEvent.data == data
    end
  end,
  on_cost = function (self, event, target, player, data)
    local cards = player.room:askToCards(player, {
      min_num = 1, max_num = 1, skill_name = skel.name, include_equip = true, cancelable = true,
      prompt = "#emo__silan-exchange:::"..data.card:toLogString()
    })
    if #cards > 0 then
      event:setCostData(self, {cards = cards})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local subcards = room:getSubcardsByRule(data.card, {Card.Processing})
    local my_cards = event:getCostData(self).cards
    room:moveCards({
      ids = subcards,
      to = player,
      toArea = Card.PlayerHand,
      moveReason = fk.ReasonExchange,
      skillName = skel.name,
      proposer = player,
    },{
      ids = my_cards,
      from = player,
      toArea = Card.Processing,
      moveReason = fk.ReasonExchange,
      skillName = skel.name,
      proposer = player,
    })
    if not player.dead and room:getCardArea(my_cards[1]) == Card.Processing and data.tos and #data.tos > 0 and not data.toCard then
      local new_card = Fk:getCardById(my_cards[1])
      local tos = table.simpleClone(data.tos)
      local prompt = "#emo__silan-muti-change:"..data.from.id.."::" ..data.card.name..":"..new_card:toLogString()
      if #tos == 1 then
        prompt = "#emo__silan-single-change:"..data.from.id..":"..tos[1].id..":" ..data.card.name..":"..new_card:toLogString()
      end
      if table.every(tos, function (to)
        return data.from:canUseTo(new_card, to, {bypass_distances = true, bypass_times = true})
      end) -- 所有目标为合法目标
      -- 装备牌和延时锦囊仅能有1个目标
      and not ((new_card.type == Card.TypeEquip or new_card.sub_type == Card.SubtypeDelayedTrick) and #tos > 1)
      and room:askToSkillInvoke(player, { skill_name = skel.name, prompt = prompt }) then
        data:removeAllTargets()
        room:useCard{
          from = data.from,
          tos = tos,
          card = new_card,
          extraUse = true,
        }
      end
    end
    room:cleanProcessingArea(my_cards)
  end,
})


Fk:loadTranslationTable{
  ["emo__silan"] = "思澜",
  [":emo__silan"] = "每回合第一张普通锦囊牌被使用时，你可以用一张牌交换此牌，若所有目标均为你交出牌的合法目标，你可以将使用的牌改为此牌。",

  ["#emo__silan-exchange"] = "思澜：你可以用一张牌交换 %arg",
  ["#emo__silan-single-change"] = "思澜：你可以将 %src 对 %dest 使用的【%arg】改为%arg2",
  ["#emo__silan-muti-change"] = "思澜：你可以将 %src 使用的【%arg】改为%arg2",
}


return skel
